import { createRouter, createWebHashHistory } from 'vue-router'
import { useAppStore } from '@/store'
import routes from './routes'
import NProgress from '@/hooks/nprogress'

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior() {
    return { top: 0 }
  }
})

/** 导航守卫 */
router.beforeEach((to, from, next) => {
  const appStore = useAppStore()
  NProgress.start()

  if (to.name === 'login') {
    next()
  } else {
    if (!appStore.isAuthenticated) {
      next('/login')
    } else {
      next()
    }
  }

  NProgress.done()
})

export default router
